package action.user;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import db.*;

@SuppressWarnings("serial")
public class EditStatusAction extends ActionSupport{
	private String status;
	private String edit_id;
	public String getStatus() {
		return status;
	}
	
	public void setStatus(String status) {
		this.status = status;
	}
	
	public String getEdit_id() {
		return edit_id;
	}

	public void setEdit_id(String editId) {
		edit_id = editId;
	}
	
	@SuppressWarnings("unchecked")
	public String edit_sc_status(){
		Map session = ActionContext.getContext().getSession();
		String userId = (String)session.get("userId");
		int uId = Integer.parseInt(userId);
		Integer scId = Integer.parseInt(this.getEdit_id());
		ServiceContractDAO scDao = new ServiceContractDAO();
		org.hibernate.Transaction trans = scDao.getSession().beginTransaction();
		ServiceContract scToEdit = scDao.findById(scId);
		if (scToEdit.getUId() != uId ){
			System.out.println("Wrong privilege!");
			return ERROR;
		}
			
		Integer current_status = scToEdit.getScStatus();
		scToEdit.setScStatus(Math.abs(current_status-1));
		scDao.save(scToEdit);
		trans.commit();
		
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String edit_tr_status(){
		Map session = ActionContext.getContext().getSession();
		String userId = (String)session.get("userId");
		int uId = Integer.parseInt(userId);
		Integer trId = Integer.parseInt(this.getEdit_id());
		TransactionDAO trDao = new TransactionDAO();
		org.hibernate.Transaction trans = trDao.getSession().beginTransaction();
		db.Transaction trToEdit = trDao.findById(trId);
		if (trToEdit.getServiceContract().getUId() != uId ){
			System.out.println("Wrong privilege!");
			return ERROR;
		}
			
		Integer current_status = trToEdit.getTrStatus();
		trToEdit.setTrStatus(Math.abs(current_status-1));
		trDao.save(trToEdit);
		trans.commit();
		
		return SUCCESS;
	}
}
